package com.sloth.service.impl;

import com.sloth.mapper.*;
import com.sloth.pojo.*;
import com.sloth.service.SeckillService;
import com.sloth.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class SeckillServiceImpl implements SeckillService {

    @Autowired
    private SeckillListMapper seckillListMapper;

    @Autowired
    private MyorderMapper myorderMapper;

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    private static int count = 0;




    @Override
    public ServerResponse getSeckillProduct(String time) {

        Date date = new Date(time);
        SeckillListExample seckillListExample = new SeckillListExample();
        seckillListExample.createCriteria().andSecStartTimeEqualTo(date);
        List<SeckillList> seckillLists = seckillListMapper.selectByExample(seckillListExample);
        ServerResponse serverResponse = ServerResponse.getSuccess(seckillLists);

        return serverResponse;
    }

    //  secId 和 custId 秒杀
    @Transactional
    @Override
    public ServerResponse seckill(int secId, int custId,int addressId) {

        ValueOperations valueOperations = redisTemplate.opsForValue();
        SeckillList seckill = seckillListMapper.selectByPrimaryKey(secId);


        SeckillListExample seckillExample = new SeckillListExample();
        seckillExample.createCriteria().andSecStockGreaterThan(0).andSecIdEqualTo(secId);
        if(seckillListMapper.reduceSeckillStock(seckill,seckillExample)==0){
            return ServerResponse.saveFail("秒杀失败");
        }
        if (seckill.getSecStock()<1){
            valueOperations.set("isStockEmpty:"+secId,"0");
            return null;
        }



        Long date = new Date().getTime();
        Date hope =new Date(date+4*24*60*60*1000);
        Myorder order=new Myorder();
        order.setSuperId(secId);
        order.setOrderNo(Long.parseLong(custId+""+date));
        order.setCustId(custId);
        order.setOrderDate(hope);
        order.setOrderPrice(seckill.getSecPrice());
        order.setAddressId(addressId);
        order.setOrderStates(0);
        order.setState(0);

        int row = myorderMapper.insert(order);


        MyorderExample myorderExample = new MyorderExample();
        myorderExample.createCriteria().andSuperIdEqualTo(secId).andCustIdEqualTo(custId);
        List<Myorder> myorderList= myorderMapper.selectByExample(myorderExample);
        int orderId = myorderList.get(0).getOrderId();


        order.setOrderId(orderId);
        OrderDetails orderDetails = new OrderDetails();
        orderDetails.setOrderId(orderId);
        orderDetails.setProId(seckill.getProId());
        orderDetails.setOdProCount(1);
        orderDetails.setOdStates(0);
        orderDetails.setOdHopeDate(new Date());
        orderDetails.setState(0);
        orderDetailsMapper.insert(orderDetails);

//
//
//        SeckillOrder seckillOrder = new SeckillOrder();
//        seckillOrder.setSecOrderNum(new Date().getTime());
//        seckillOrder.setCustId(custId);
//        seckillOrder.setSecId(secId);
//        seckillOrder.setSecOrderDate(new Date());
//        seckillOrder.setSecOrderPrice(seckill.getSecPrice());
//        seckillOrder.setAddressId(5);
//        seckillOrder.setState(0);
//        int row = seckillOrderMapper.insert(seckillOrder);

        if (row!=0){
            valueOperations.set("order:"+custId+":"+secId,order);
        }else {
            return ServerResponse.saveFail("创建订单失败");
        }


        return ServerResponse.saveSuccess("秒杀成功");

    }

    @Override
    public SeckillList getSeckill(int secId) {
        SeckillList seckill = seckillListMapper.selectByPrimaryKey(secId);
        if (seckill!=null){
            return seckill;
        }
        return null;
    }

    @Override
    public List<SeckillList> getAllSeckill() {
        List<SeckillList> list = seckillListMapper.selectByExample(null);
        return list;
    }

    @Override
    public int getResult(Integer custId, int secId) {
        MyorderExample orderExample = new MyorderExample();
        orderExample.createCriteria().andCustIdEqualTo(custId).andSuperIdEqualTo(secId);
        List<Myorder> list = myorderMapper.selectByExample(orderExample);
        if (list.size()>0){
            return list.get(0).getOrderId();
        }else if (redisTemplate.hasKey("isStockEmpty:"+secId)){
            return -1;
        }else {
            return 0;
        }
    }

    // 通过秒杀商品id查询秒杀商品信息
    @Override
    public Product getProductBySeckillProductId(Integer seckillProductId) {

//        SeckillOrder seckillOrder = seckillOrderMapper.selectByPrimaryKey(seckillOrderId);

        SeckillList seckillList = seckillListMapper.selectByPrimaryKey(seckillProductId);

        Product product = productMapper.selectByPrimaryKey(seckillList.getProId());

        return product;
    }

    // 通过 秒杀商品id 查询到 秒杀商品价格
    @Override
    public Double getSeckillProductPriceBySeckillProductId(Integer seckillProductId) {

//        SeckillOrder seckillOrder = seckillOrderMapper.selectByPrimaryKey(seckillOrderId);

        SeckillList seckillList = seckillListMapper.selectByPrimaryKey(seckillProductId);

        return seckillList.getSecPrice();
    }

    // 通过订单id查询订单中商品
    @Override
    public Product getProductNameBySeckillOrderId(Integer seckillOrderId) {
        OrderDetailsExample orderDetailsExample = new OrderDetailsExample();
        orderDetailsExample.createCriteria().andOrderIdEqualTo(seckillOrderId);
        OrderDetails orderDetails = orderDetailsMapper.selectByExample(orderDetailsExample).get(0);
        Product product = productMapper.selectByPrimaryKey(orderDetails.getProId());
        return product;
    }


    // 通过 订单id 在订单详情表中查询出属于这个 订单id 的订单详情信息，因为一个订单对应一个商品对应一个订单详情
    @Override
    public OrderDetails getOrderDetailsByOrderId(Integer orderId) {
        OrderDetailsExample orderDetailsExample = new OrderDetailsExample();
        orderDetailsExample.createCriteria().andOrderIdEqualTo(orderId);
        OrderDetails orderDetails = orderDetailsMapper.selectByExample(orderDetailsExample).get(0);
        return orderDetails;
    }

    // SeckillVO 获得订单编号、商品总价格、订单中商品名称，通过 订单id 获得
    @Override
    public SeckillVO getSeckillVOBySeckillOrderId(Integer seckillOrderId) {
        Myorder myorder = myorderMapper.selectByPrimaryKey(seckillOrderId);

        OrderDetailsExample orderDetailsExample = new OrderDetailsExample();
        orderDetailsExample.createCriteria().andOrderIdEqualTo(myorder.getOrderId());
        OrderDetails orderDetails = orderDetailsMapper.selectByExample(orderDetailsExample).get(0);
        Product product = productMapper.selectByPrimaryKey(orderDetails.getProId());

        SeckillVO seckillVO = new SeckillVO();
        seckillVO.setSeckillNO(myorder.getOrderNo());
        seckillVO.setSekillPrice(myorder.getOrderPrice());
        seckillVO.setSeckillProduct(product.getProName());

        return seckillVO;
    }


}
